<? require_once $_SERVER["DOCUMENT_ROOT"] . "/frostcore/header.php"; ?>
<?
$user = $USER->GetUserInfo();
if (check_action('update_user', 'post')) {
    $fields = array();
    
    if (!empty($_FILES['field'])) {
        foreach ($_FILES['field']["name"] as $i => $empty) {
            foreach ($empty as $j => $d) {
                if(!$d){
                    continue;
                }
                $file = array(
                    "name" => $empty[$j],
                    "tmp_name" => $_FILES['field']['tmp_name'][$i][$j],
                    "size" => $_FILES['field']['size'][$i][$j],
                    "type" => $_FILES['field']['type'][$i][$j]
                );
                $vals = array(
                    "field_id" => $i,
                    "value" => Filesystem::Upload(array($file)),
                    "id" => $j,
                    'user_id' => $user['id'],
                    'date_create' => "NOW()"
                );
                if ($j) {
                    unset($vals['date_create']);
                    $vals['id'] = $j;
                }
                $fields[] = $vals;
            }
        }
    }
    foreach ($_POST['field'] as $fid => $data) {
        if (is_array($data)) {
            foreach ($data as $vid => $value) {
                $vals = array(
                    'field_id' => $fid,
                    'value' => $value,
                    'user_id' => $user['id'],
                    'date_create' => "NOW()"
                );
                if ($vid) {
                    unset($vals['date_create']);
                    $vals['id'] = $vid;
                }
                $fields[] = $vals;
            }
        }
    }
    BDS::Init('users__values');
    foreach ($fields as $fld) {
        if (!empty($fld['id'])) {
            $id = $fld['id'];
            unset($fld['id']);
            BDS::Update($id, $fld);
        } else {
            BDS::Create($fld);
        }
    }
}
Module::Init('system', 'fields');
$user = $USER->GetUserInfo();
$fields = $user['fields'];
unset($user['fields'], $user['group']);
?>
<form method="post" enctype="multipart/form-data">
    <?
    set_action("update_user");
    foreach ($fields as $field) {
        print FieldConstructor::GetFieldHtml($field);
    }
    ?>    
    <input type="submit">
</form>

<? require_once $_SERVER["DOCUMENT_ROOT"] . "/frostcore/footer.php"; ?>